package com.orangelife.service.bill.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.base.BaseConfig;
import com.orangelife.service.bill.IBillService;
import com.orangelife.service.drool.BillDrools;
import com.orangelife.service.drool.BillParms;
import com.orangelife.service.feeDetail.IFeeDetailService;
import com.orangelife.service.propFeeCommInfo.IPropFeeCommInfoService;
import com.orangelife.service.propFeeCommInfo.model.PropFeeCommInfoDto;
import com.orangelife.service.propFeeMeter.IPropFeeMeterService;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.bill.entity.BillDto;

@Service("billService")
public class BillService extends MybaitsGenericDaoSubLogic implements
		IBillService {
	@Resource(name="feeDetailService")
private IFeeDetailService feeDetailService;
	@Resource(name = "propFeeCommInfoService")
	private IPropFeeCommInfoService propFeeCommInfoService;
	@Resource(name = "propFeeMeterService")
	private IPropFeeMeterService propFeeMeterService;
	public static Map commParam=null; 

	/**
	 * 小区生成账单状态
	 * 
	 * */
	public static Map<String,String> commFeeStatus=new HashMap<String,String>(); 
	
	
	
	public static void main(String[] args) {
		
		System.out.println(3100605080107090402l%100);
		// Double dd= Double.valueOf("9.86");
		// BigDecimal bigD = new BigDecimal("9.86");
		// bigD = bigD.multiply(new BigDecimal(100));
		// Long result = bigD.longValue();
		// System.out.println(bigD.toString());
	}
	
	/**
	 * 小获取小区生成状态  1为锁定状态,，
	 * 返回boolean
	 * */
	public static boolean getFeeStatus(String commID){
		synchronized(commFeeStatus){		
			if(commFeeStatus.get(commID)==null){			
			return false;			
			}else if("1".equals(commFeeStatus.get(commID))){			
				return true;			
			}			
			return false;						
		}			
	} 
	
	
	/**
	 * 设置当状态为非锁定状态时设置为锁定状态,
	 * 返回boolean
	 * */
	public static  boolean setFeeStatusLock(String commID){
		synchronized(commFeeStatus){		
		if("1".equals(commFeeStatus.get(commID))){			
				return false;									
		}
		else{
			
			commFeeStatus.put(commID,"1");
			return true;	
		}	
	
	
		}			
	} 
	
	/**
	 * 设置当状态为锁定状态时设置为非锁定状态,
	 * 返回boolean
	 * */
	public static boolean setFeeStatusUnlock(String commID){
		synchronized(commFeeStatus){		
		if("1".equals(commFeeStatus.get(commID))){	
			commFeeStatus.put(commID, "0");
			
				return true;			
						
		}
		return true;	
	
	
		}			
	} 
	
	
	
	@Override
	public List<Map<String, Object>> creFixFeeDetail(String apartmentID, String commID,String[] feeTypeIDs,
			String startTime, String endTime) throws AppBizException {
		
		BillParms billParms = new BillParms(); 
		//设置公共配置参数
		Map<String,Object> commConfigParms = new HashMap<String,Object>();
		//设置计算参数
		Map<String,String> commParms = new HashMap<String,String>();
		//设置小区ID
		commConfigParms.put("commID",commID);
		commParms.put("commID",commID);
		List<Map<String,Object>> apartList=null;
	
	    Map<String,Object> apartmentInfo=null;
		
		   String [] alist={apartmentID};
        //获取房屋信息
		 apartList=this.getApartmentInfo(alist);
		 
		 if(apartList==null||apartList.size()==0){
				
				
				return  null;
			}
		 
		 else{
			 apartmentInfo= apartList.get(0);
			 
		 }
		 List<Map<String,String>> rules=null;
			 rules=this.getFixRuleList(StringUtils.toString(apartmentInfo.get("bill_type_id")),commID, feeTypeIDs);
				
			KieSession kieSession	=BillDrools.getKieSessionbyCommID(commID);
			int n=0;
			
			
			List<Map<String, Object>> res=new ArrayList<Map<String, Object>>();
			
			for (Map<String, String> rule : rules) {
				String rule_param = StringUtils.toString(rule.get("rule_param"));
				
				
				Map<String, String> params = new HashMap<String, String>();
				params = this.setParams(rule_param, commParms, apartmentInfo,StringUtils.toString(rule.get("fee_type_id")),startTime);	
				
				params.put("apartmentID",apartmentInfo.get("apartment_id").toString());
				
				params.put("buildingID",apartmentInfo.get("building_id").toString());
				params.put("rule", rule.get("rule"));
				//加载规则
				billParms.init();
				billParms.setParams(params);
				billParms.setType(rule.get("rule_type"));
			
					FactHandle l= 	kieSession.insert(billParms);
					kieSession.fireAllRules();
				kieSession.delete(l);
					billParms.getRes();
				
			       Map<String,Object> r=new HashMap<String,Object>();
					r.put("feeTypeID",StringUtils.toString(rule.get("fee_type_id")));	
					r.put("feeTypeName",StringUtils.toString(rule.get("fee_type_name")));	
					
					if(billParms.getRes()==null){
						
						continue;
					}
					r.put("amount",billParms.getRes().toString());	
					res.add(r);
				}
				
		 
		 
		 return res;
		 
		
	}
	
	
	

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public void startCreFeeDetail(BillDto billDto) throws AppBizException {
		
		
		String commID=billDto.getCommID();
		BillParms billParms=new BillParms(); 
		//设置公共配置参数
		Map<String,Object> commConfigParms=new HashMap<String,Object>();
		//设置计算参数
		Map<String,String> commParms=new HashMap<String,String>();
		
		//设置小区ID
		commConfigParms.put("commID",billDto.getCommID());
		commParms.put("commID",billDto.getCommID());
		
	
		
		//要生成的房屋信息 1根据房屋id,2根据单元，3根据楼栋，4根据小区
		String type="4";
		if(billDto.getApartmentIDs()!=null&&billDto.getApartmentIDs().length>0){
			type="1";
			
			
			
		}else if(!StringUtils.isBlank(billDto.getUnitID())){
			
			type="2";	
		}else if(!StringUtils.isBlank(billDto.getBuildingID())){
			
			type="3";	
		}
		
		List<Map<String,Object>> apartList=null;
		   if("1".equals(type)){
				
			 apartList=this.getApartmentInfo(billDto.getApartmentIDs());		
		
	            }
            if("4".equals(type)){
         apartList=this.getApartmentInfoListByCommID(commID);
	
	
            }
if(apartList==null){
	
	
	return ;
}
	
		if(apartList!=null&&apartList.size()>0){
			
			KieSession kieSession	=BillDrools.getKieSessionbyCommID(commID);
			int n=0;
			for (Map<String, Object> apartInfo : apartList) {
				
				
				n++;
				if(n==10){
					
//					break;
				}
			//==================单房屋生成缴费账单==================
				//获取收费类型
				String billTypeID = StringUtils.toString(apartInfo.get("bill_type_id"));
				//在公共参数中查看是否存在该配置
				//获取收费规则==================
				List<Map<String,String>> rules=null;
				if(commConfigParms.get(commID+"_"+billTypeID)!=null){
					
					rules=(List<Map<String,String>>)commConfigParms.get(commID+"_"+billTypeID);
					
					
				}else{		
					rules=this.getRuleList(billTypeID, commID);
					commConfigParms.put(commID+"_"+billTypeID,rules);
					//获取该小区下该收费类型的缴费项和缴费规则；
					
					
					
					
				}
				
				//获取收费规则==================
				
				//根据收费规则生成缴费账单==================
				
		
				int n1=0;
				for (Map<String, String> rule : rules) {
					
					n1++;

					String rule_param = StringUtils.toString(rule.get("rule_param"));
					
					
					Map<String, String> params = new HashMap<String, String>();
					params = this.setParams(rule_param, commParms, apartInfo,StringUtils.toString(rule.get("fee_type_id")),billDto.getStartTime());	
					
					params.put("apartmentID",apartInfo.get("apartment_id").toString());
					
					params.put("buildingID",apartInfo.get("building_id").toString());
					params.put("rule", rule.get("rule"));
					//加载规则
					billParms.init();
					billParms.setParams(params);
					billParms.setType(rule.get("rule_type"));
				
						FactHandle l= 	kieSession.insert(billParms);
						System.out.println(kieSession.getFactCount()+"**********FactCount");
					
						kieSession.fireAllRules();
						kieSession.delete(l);
						
					if(billParms.getRes()!=null&&BigDecimal.ZERO.compareTo(billParms.getRes())<0){
						
						Map <String ,Object> p=new HashMap<String,Object>();
						 p.put("feeDetailID", IdUtil.getuuId());
						 p.put("apartmentID", apartInfo.get("apartment_id"));
						 p.put("feeTypeID", rule.get("fee_type_id"));
						 
						 
						 p.put("amount",billParms.getRes().setScale(Integer.parseInt(BaseConfig.getRound(commID)), BigDecimal.ROUND_HALF_UP).toString());
						 
						 p.put("realamount",billParms.getRes().setScale(3, BigDecimal.ROUND_HALF_UP).toString());
						 
					     p.put("feeAmount", billParms.getAmount());
					     p.put("feePrice",  billParms.getPrice());
					     
					     System.out.println( billParms.getPrice()+"**********"+ billParms.getAmount());
					     p.put("feeDate", billDto.getStartTime());
						 p.put("status", "0");
						 p.put("itemStat", "1");
					     p.put("planID",billDto.getPlanID());
						 
					 	System.out.println(rule.get("fee_type_name")+"****"+"*********"+billParms.getRes());
				      int r= this.addFeeDetail(p);
				      if(r==0){
				    	  
				    	  feeDetailService.updateType(apartInfo.get("apartment_id").toString(), rule.get("fee_type_id"), billDto.getStartTime(), "1",billDto.getPlanID());
				    	  
				      }
//						System.out.println(billParms.getRes().setScale(3, BigDecimal.ROUND_HALF_UP));
					}
					
				
		

					
					
					
				}
				
				
				
				
				
				//根据收费规则生成缴费账单==================
				
				//==================单房屋生成缴费账单==================
				
			}
		
		}
		
		
		
		
		
		
		
		
		
		
		
		
		//更新该小区的账单生成状态为锁转态
		
		
		// TODO Auto-generated method stub
		
	}

	
	
	
	

	@Override
	public void creFeeDetail(String commID,String buildID,String unitID,String apartmentID, String feeTypeID,
			String startTime, String endTime) throws AppBizException {
////		if(1==1){
////			
////			Test t=new Test();
////			t.bb();
////			return ;
////			
////		}
////		
//	
//
//		// 获取房屋信息
//		Map<String, Object> apartInfo = this.getApartmentInfo(apartmentID);
//		
//		
//		String billTypeID = StringUtils.toString(apartInfo.get("bill_type_id"));
//		
//		
//		String feeArea = StringUtils.toString(apartInfo.get("fee_area"));
//		apartInfo.put("comm_id", "0493de40-1722-4aa7-843a-c64803996990");
//		if (StringUtils.isBlank(billTypeID)) {
//
//			return;
//		}
//		
//	
//		
//		
//	
//		
//		
//		
//		// 获取缴费规则
//		
//		
//
//		Map<String, Object> ruleinfo = this.getRule(feeTypeID, billTypeID);
//		
//		
//		
//
//		String rule_param = StringUtils.toString(ruleinfo.get("rule_param"));
//
//		String type = StringUtils.toString(ruleinfo.get("rule_type"));
//		String rule = StringUtils.toString(ruleinfo.get("rule"));
//		// 获取所需参数
//		Map<String, String> commparam = new HashMap<String, String>();
//		Map<String, String> params = new HashMap();
//		
//		String date="2016-03";
//		params = this.setParams(rule_param, commparam, apartInfo,date);
//		
//		// 用获取的参数进行计算
//		BigDecimal mmm = this.compute(type, rule, params);
//		
//		
//		System.out.println(this.compute(type, rule, params)
//				+ "*******************"
//				+ mmm.setScale(1, BigDecimal.ROUND_HALF_UP).toString());
//		
//		//插入缴费细项
//		
//		Map <String ,Object> p=new HashMap<String,Object>();
//		 p.put("feeDetailID", IdUtil.getuuId());
//		 p.put("apartmentID", apartmentID);
//		 p.put("feeTypeID", feeTypeID);
//		 p.put("amount",mmm.toString());
//		 p.put("feeAmount", "1212");
//		 p.put("feePrice", "0.533");
//		 p.put("timetype", "0");
//		 p.put("feeDate", date);
//		 p.put("status", "0");
//		 p.put("itemStat", "1");
//		 p.put("plantID", "011111111111111");
//		 
//		 
//this.addFeeDetail(p);
//
//		// TODO Auto-generated method stub

	}

	@Override
	public void creFeeDetail(String apartmentID, String[] feeTypeID,
			long startTime, long endTime) throws AppBizException {

		// TODO Auto-generated method stub

	}

	@Override
	public void creFeeDetail(String[] apartmentIDs, String[] feeTypeIDs,
			long startTime, long endTime) throws AppBizException {

		// TODO Auto-generated method stub

	}
//根据房屋ID获取房屋信息
	public Map<String, Object> getApartmentInfo(String apartmentID) {

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("apartmentID", apartmentID);

		Map<String, Object> res = this.getSqlSession().selectOne(
				"BillMapper.getApartmentInfo", param);

		return res;

	}
	
	
	
	//根据房屋ID数组获取房屋信息
	public List<Map<String, Object>> getApartmentInfo(String[] apartmentIDs) {
           if(apartmentIDs==null||apartmentIDs.length==0){
	
	
	           return null;
          }
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("apartmentIDs", apartmentIDs);

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"BillMapper.getApartmentInfoList", param);

		return res;

	}
	//根据房屋ID数组获取房屋信息
	public List<Map<String, Object>> getApartmentInfoListByCommID(String commID) {
           if(commID==null){
	
	
	           return null;
          }
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", commID);

		List<Map<String, Object>> res = this.getSqlSession().selectList(
				"BillMapper.getApartmentInfoListByCommID", param);

		return res;

	}
	
	

	public Map<String, Object> getRule(String feeTypeID, String billTypeID) {

		if (StringUtils.isBlank(feeTypeID) || StringUtils.isBlank(billTypeID)) {

			return null;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("feeTypeID", feeTypeID);
		param.put("billTypeID", billTypeID);

		Map<String, Object> res = this.getSqlSession().selectOne(
				"BillMapper.getRule", param);

		return res;

	}
	
	public List<Map<String, String> >getRuleList(String billTypeID, String commID) {

		if (StringUtils.isBlank(billTypeID) || StringUtils.isBlank(commID)) {

			return null;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("billTypeID", billTypeID);
		param.put("commID", commID);

		List<Map<String, String>> res = this.getSqlSession().selectList(
				"BillMapper.getRuleList", param);

		return res;

	}
	

	public BigDecimal compute(String type, String rule,
			Map<String, String> params) {

		switch (type) {
		case "1":
			BigDecimal mm = new BigDecimal(rule).multiply(new BigDecimal(params
					.get("feeArea")));

			return mm;

		case "2":
			return new BigDecimal(rule);

		case "3":
			return new BigDecimal(params.get("01"))
					
					.multiply(new BigDecimal("0.533"))
					.multiply(new BigDecimal(params.get("ratio"))).divide(new BigDecimal(params.get("ratiosum")),3);
		case "4":
			System.out.println(params.get("04")+"***********************");
			return new BigDecimal(params.get("04"))
			.multiply(new BigDecimal("2.08"))
			.multiply(new BigDecimal(params.get("feeArea"))).divide(new BigDecimal(params.get("bill_4_5_feeArea")),3);
		case "5":
		
			return new BigDecimal(params.get("02"))
			.multiply(new BigDecimal("0.533"))
			.multiply(new BigDecimal(params.get("feeArea"))).divide(new BigDecimal(params.get("unit_feeArea")),3);
		case "6":
			
			return new BigDecimal(params.get("05"))
			.multiply(new BigDecimal("0.533"))
			.multiply(new BigDecimal(params.get("feeArea"))).divide(new BigDecimal(params.get("bill_4_5_feeArea")),3);
		default:
			break;
		}

		return null;

	}

	public Map<String, String> setParams(String param,
			Map<String, String> commparam, Map<String, Object> apartInfo,String feeTypeID,String date) {
		
	
		Map<String, String> m = new HashMap<String, String>();
		
		if(param==null){
			
			return m;
		}
		String[] params = param.split("##");

		if (params != null && params.length > 0) {

			for (String string : params) {

				m.put(string, this.getParams(string, commparam, apartInfo,feeTypeID,date));

			}
		}

		return m;

	}

	public String getParams(String param, Map<String, String> commparam,
			Map<String, Object> apartInfo,String feeTypeID,String date) {
		//m_comm__01小区公摊表读数 m_unit单元公摊表读数,m_build 
		//m_sum__07 获取关联表总和,
		//m_area_  m_comm_area 小区表关联总面积 m_unit_area,m_build_area
		//m_comm_amount_01  小区表关联表总数 m_unit_amount_01,,m_build_amount_01 ,
		//rule 根据规则获取缴费项目值
		
		String type=param;
		
		String pp=param;
		
		if(param.indexOf("__")!=-1){
			
			String[] lll=param.split("__");
			
			 type=lll[0];
			 
			 if(lll.length==2){
				 
				 pp=lll[1]; 
			 }
			
		}
		
	
		
		
		

		switch (type) {
		
		case "rule_val":
			
			
			return this.getRuleParam(feeTypeID, apartInfo.get("apartment_id").toString());
		
		case "ratiosum":

			return "220";
		case "feeArea":

			return apartInfo.get("fee_area").toString();
		case "bill_4_5_feeArea":
			
			
			if (commparam.get("bill_4_5_feeArea") == null) {

				return this.getAreaByBillType();

			} else {

				return commparam
						.get("bill_4_5_feeArea");

			}
           case "unit_feeArea":
			
			
			if (commparam.get("unit_feeArea"+apartInfo.get("unit_id").toString()) == null) {

				return this.getUnitArea(apartInfo.get("unit_id").toString());

			} else {

				return commparam
						.get("unit_feeArea"+apartInfo.get("unit_id").toString());

			}
			
	
		case "ratio":
			int n = Integer.parseInt(apartInfo.get("floor").toString());

			if (n < 6) {

				return "1";
			}
			if (n < 11) {

				return "1.2";
			}
			if (n < 16) {

				return "1.4";
			}
			if (n < 21) {

				return "1.6";
			}
			if (n < 26) {

				return "1.8";
			}
			if (n < 31) {

				return "2";
			}

		case "floor":

			return apartInfo.get("floor").toString();

		case "m_unit":

			if (commparam.get("m_unit"+pp + apartInfo.get("unit_id").toString()) == null) {

				String lll= this.getMetterRecordByAttrType(pp, "2",
						apartInfo.get("unit_id").toString(), date);
				
				
				
				commparam.put("m_unit"+pp +apartInfo.get("unit_id").toString(),lll);
				return lll;

			} else {

				return commparam
						.get("m_unit"+pp  + apartInfo.get("unit_id").toString());

			}
		case "m_apart":

			if (commparam.get("m_apart"+pp + apartInfo.get("apartment_id").toString()) == null) {

				String lll= this.getMetterRecordByAttrType(pp, "3",
						apartInfo.get("apartment_id").toString(), date);
				
				
				
				commparam.put("m_apart"+pp +apartInfo.get("apartment_id").toString(),lll);
				return lll;

			} else {

				return commparam
						.get("m_apart"+pp  + apartInfo.get("apartment_id").toString());

			}
			
		case "m_comm":

			if (commparam.get("m_comm"+pp +commparam.get("commID")) == null) {

				String lll= this.getMetterRecordByAttrType(pp, "0",
						commparam.get("commID"), date);
				
				commparam.put("m_comm"+pp + commparam.get("commID"),lll);
				return lll;

			} else {

				return commparam.get("m_comm"+pp + commparam.get("commID"));

			}
		case "m_unit_area":

			if (commparam.get("m_unit_area"+pp +apartInfo.get("unit_id").toString()) == null) {

				String lll= this.getMeterAreaByAttrType(pp, "2",
						apartInfo.get("unit_id").toString());
				
				commparam.put("m_unit_area"+pp + apartInfo.get("unit_id").toString(),lll);
				
				return lll;
				

			} else {

				return commparam.get("m_unit_area"+pp + apartInfo.get("unit_id").toString());

			}
		case "m_build_area":

			if (commparam.get("m_build_area"+pp +apartInfo.get("building_id").toString()) == null) {

				String lll= this.getMeterAreaByAttrType(pp, "1",
						apartInfo.get("building_id").toString());
				
				commparam.put("m_build_area"+pp + apartInfo.get("building_id").toString(),lll);
				
				return lll;
				

			} else {

				return commparam.get("m_build_area"+pp + apartInfo.get("building_id").toString());

			}
		case "m_comm_area":

			if (commparam.get("m_comm_area"+pp +commparam.get("commID")) == null) {

				String lll= this.getMeterAreaByAttrType(pp, "0",
						commparam.get("commID"));
				
				commparam.put("m_comm_area"+pp + commparam.get("commID"),lll);
				
				return lll;
				

			} else {

				return commparam.get("m_comm_area"+pp + commparam.get("commID"));

			}
			
			
		case "m_comm_amount":
			
			if (commparam.get("m_comm_amount"+pp +commparam.get("commID")) == null) {

				String lll= this.getMeterSumAmountByAttrType(pp, "0",
						commparam.get("commID"),date);
				
				commparam.put("m_comm_amount"+pp + commparam.get("commID"),lll);
				
				return lll;
				

			} else {

				return commparam.get("m_comm_amount"+pp + commparam.get("commID"));

			}
	       case "m_build_amount":
			
			if (apartInfo.get("m_build_amount"+pp +apartInfo.get("building_id")) == null) {

				String lll= this.getMeterSumAmountByAttrType(pp, "1",
						apartInfo.get("building_id").toString(),date);
				
				commparam.put("m_build_amount"+pp + apartInfo.get("building_id").toString(),lll);
				
				return lll;
				

			} else {

				return commparam.get("m_build_amount"+pp + apartInfo.get("building_id").toString());

			}
			
        case "m_unit_amount":
			
			if (commparam.get("m_unit_amount"+pp +apartInfo.get("unit_id").toString()) == null) {

				String lll= this.getMeterSumAmountByAttrType(pp, "2",
						apartInfo.get("unit_id").toString(),date);
				
				commparam.put("m_unit_amount"+pp +apartInfo.get("unit_id").toString(),lll);
				
				return lll;
				

			} else {

				return commparam.get("m_unit_amount"+pp + apartInfo.get("unit_id").toString());

			}
			

		case "m_sum":

			if (commparam.get("m_sum"+pp + commparam.get("commID")) == null) {

					String lll=this.getSumAmountByMeterID(pp, date);
					
					commparam.put("m_sum"+pp + commparam.get("commID"),lll);
					return lll;
			} else {

				return commparam.get("m_sum"+pp + commparam.get("commID"));

			}
	

		default:

			break;
		}

		return null;
	}

	public String getMetterRecordByAttrType(String attrType, String belongType,
			String belongID, String period) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("attrType", attrType);
		param.put("belongType", belongType);
		param.put("belongID", belongID);
		param.put("period", period);
		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getMetterRecordByAttrType", param);

		if(mm==null){
			
			return null;
		}
		return StringUtils.toString(mm.get("use_amount"));

	}
	public String getAreaByBillType() {

	
		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getAreaByBillType");

		return StringUtils.toString(mm.get("totalArea"));

	}
	public String getUnitArea(String unitID) {


		Map<String, String> param = new HashMap<String, String>();
		param.put("unitID", unitID);
		

		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getAreaByUnit",param);

		return StringUtils.toString(mm.get("totalArea"));

	}
	
	
	
	//根据属性获取房屋面积
	public String getMeterAreaByAttrType(String attrType, String belongType,
			String belongID) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("attrType", attrType);
		param.put("belongType", belongType);
		param.put("belongID", belongID);
	
		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getMetterByAttrType", param);

		if(mm==null){
			
			return null;
		}

		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("meterID", mm.get("meter_id"));
		

		Map<String, Object> mmm = this.getSqlSession().selectOne(
				"BillMapper.getSumAreaByMeterID",param1);
if(mmm==null){
	
	
	return null;
}
		return StringUtils.toString(mmm.get("sumArea"));

	}
	
	
	//根据id获取房屋面积
	public String getMeterArea(String meterID) {


		Map<String, String> param = new HashMap<String, String>();
		param.put("meterID", meterID);
		

		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getSumAreaByMeterID",param);

		return StringUtils.toString(mm.get("sumArea"));

	}
	
	
	//根据属性表读数总和
	//根据属性获取房屋面积
	public String getMeterSumAmountByAttrType(String attrType, String belongType,
			String belongID,String period) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("attrType", attrType);
		param.put("belongType", belongType);
		param.put("belongID", belongID);
	
		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getMetterByAttrType", param);

		if(mm==null){
			
			return null;
		}

		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("meterID", mm.get("meter_id"));
		param1.put("period", period);

		Map<String, Object> mmm = this.getSqlSession().selectOne(
				"BillMapper.getSumAmountByMeterID",param1);
if(mmm==null){
	
	
	return null;
}
		return StringUtils.toString(mmm.get("sumAmount"));

	}
	//根据id获取表读数总和
	public String getSumAmountByMeterID(String meterID,String period) {


		Map<String, String> param = new HashMap<String, String>();
		param.put("meterID", meterID);
		
		param.put("period", period);
		Map<String, Object> mm = this.getSqlSession().selectOne(
				"BillMapper.getSumAmountByMeterID",param);

		return StringUtils.toString(mm.get("sumAmount"));

	}
	
	public int  updateMerRecordStatus(String commID,String bstatus,String astatus,String itemStat) {


		Map<String, String> param = new HashMap<String, String>();
		param.put("commID", commID);
		param.put("astatus", astatus);
		param.put("bstatus", bstatus);
		param.put("itemStat", itemStat);
		int l = this.getSqlSession().update(
				"BillMapper.updateMerRecordStatus",param);

		return l;

	}
	
	/**
	 * 
	 * 添加缴费细项
	 * */
	public int addFeeDetail(Map<String,Object> param){

		
		return this.getSqlSession().insert("CollectionItemMapper.insertfeedetail2", param);
		
		
		
	}

	
	
	
	
	@Override
	public List<Map<String, Object>> getPastFeedetail(String commID)
			throws AppBizException {
		
		Map<String, String> param = new HashMap<String, String>();
		param.put("commID", commID);
		return this.getSqlSession().selectList("BillMapper.getHasCreInfo", param);
	}

	@Override
	public List<Map<String, Object>> getLastFinishPlan(String commID, Date date)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", commID);
		param.put("finishTime", date);
		return this.getSqlSession().selectList("BillMapper.getLastFinishPlan", param);
	}

	@Override
	public List<Map<String, Object>> checkMeter(String commID,String period)
			throws AppBizException {
		//检测是否满足生存条件
		Map<String,Object> param=new HashMap<String,Object>();
		
		
		param.put("commID",commID);
		param.put("period",period);
		List <Map<String,Object>> res=this.getSqlSession().selectList("BillMapper.checkMeterGroup", param);
		
		return res;
	}

	@Override
	public int crePlan(Map<String, Object> param) throws AppBizException {

		return this.getSqlSession().insert("PropNoticeMapper.insertpropfeeprocplan", param);

	}

	@Override
	public int updatePlan(Map<String, Object> param) throws AppBizException {
		
		
		return this.getSqlSession().update("PropNoticeMapper.updatepropfeeprocplan", param);
	}

	@Override
	public int delPropFeeDetailByPlanID(Map<String, Object> param)
			throws AppBizException {
		return this.getSqlSession().update("BillMapper.delPropFeeDetailByPlanID", param);

	}

	@Override
	public List<Map<String, Object>> getprintplan(String commID,
			String planType, String pageIndex, String pageSize)
			throws AppBizException, ParseException {
		// TODO Auto-generated method stub
				//获取该房屋的欠费信息
				PageHelper.startPage(Integer.valueOf(pageIndex), Integer.valueOf(pageSize));
				Map<String, Object> planmap = new HashMap<String, Object>();
				planmap.put("commID",commID);
				planmap.put("planType",planType);
				planmap.put("pageIndex",pageIndex);
				planmap.put("pageSize",pageSize);
				List<Map<String, Object>> fwqflist = this.getSqlSession().selectList(
						"BillMapper.getPlan", planmap);
				return fwqflist;
		
	}

	@Override
	public List<Map<String, Object>> checkPlan(String planID)
			throws AppBizException {
		
	
				Map<String,Object> param=new HashMap<String,Object>();
				
				
				param.put("planID",planID);
				List <Map<String,Object>> res=this.getSqlSession().selectList("BillMapper.checkPlan", param);
				
				return res;
	}

	@Override
	public Map<String, Object> getPlanDetail(String planID)
			throws AppBizException {
		
		

		Map<String,Object> param=new HashMap<String,Object>();
		
		
		param.put("planID",planID);
		Map<String,Object> ll=this.getSqlSession().selectOne("BillMapper.getPlanDetail",param);
		
		// TODO Auto-generated method stub
		return ll;
	}



	public String getRuleParam(String feeTypeID,String apartmentID) {
		
		

		Map<String,Object> param=new HashMap<String,Object>();
		
		
		param.put("feeTypeID",feeTypeID);
		param.put("apartmentID",apartmentID);
		Map<String,Object> ll=this.getSqlSession().selectOne("BillMapper.getRuleByfeeTypeID",param);
		
		
		
		// TODO Auto-generated method stub
		
		if(ll==null){
			
			return null;
		}
		return StringUtils.toString(ll.get("rule"));
	}











	public List<Map<String, String> >getFixRuleList( String billTypeID,String commID,String[] feeTypeIDs) {

		
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commID", commID);
		param.put("billTypeID", billTypeID);
		param.put("feeTypeIDs", feeTypeIDs);
		List<Map<String, String>> res = this.getSqlSession().selectList(
				"BillMapper.getFixRuleList", param);

		return res;

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public void canclePlan(String planID) throws AppBizException {
		Map<String, Object> res = this.getPlanDetail(planID);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("planID", planID);

		this.delPropFeeDetailByPlanID(m);
		
		 feeDetailService.updateTypeByPlanID(null, null, null, "2", planID);
		m.put("planid", planID);
		m.put("plan_status", "5");

		this.updatePlan(m);
       
		String commID = res.get("comm_id").toString();

		
	     Map<String,Object> param=new HashMap<String,Object>();
 		param.put("commID",commID);
 		param.put("code", "curDate");
 	List<Map<String,Object>> p=propFeeCommInfoService.bauthfindPropFeeCommInfoByMap(param);
 		
 		if(p==null||p.size()==0||p.size()>1){
 			
 		throw new AppBizException(100001, "参数未配置");
 			
 		}
 		
 	
 		Map<String, Object> res1=p.get(0);
	if(!res.get("cur_date").toString().equals(res1.get("val").toString())){
 			
 			
 			throw new AppBizException(100001, "不是最新生成账单，不能撤销");
 			
 		}
 		
 		PropFeeCommInfoDto pp=new PropFeeCommInfoDto();
 		pp.setCommID(commID);
 		pp.setInfoID(res1.get("info_id").toString());
 		pp.setVal(res.get("last_date").toString());
 		
 		propFeeMeterService.updateMerRecordStatus(commID, null,"0", "7", "0"); 
 		propFeeMeterService.updateMerRecordStatus(commID,res.get("cur_date").toString(), null, "0", null);
 		propFeeMeterService.updateMerRecordStatus(commID,res.get("last_date").toString(), null, "1", null);
         propFeeCommInfoService.editPropFeeCommInfo(pp);  
		
		
	}

	@Override
	public List<Map<String, Object>> getNewPropFeeBill(List<String> commIDs,String endDate) throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commIDs", commIDs);
		param.put("endDate", endDate);
		return this.getSqlSession().selectList("BillMapper.getNewPropFeeBill", param);
	}

	@Override
	public List<Map<String, Object>> getLastFinishPlans(List<String> commIDs,
			Date date) throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commIDs", commIDs);
		param.put("finishTime", date);
		return this.getSqlSession().selectList("BillMapper.getLastFinishPlans", param);
	}








}
